package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import util.DataUtil;

import model.ThanhVienModel;

public class ThanhVienDAO {

	public static ThanhVienModel getThanhVien(String tenDangNhap, String matKhau) {
		ThanhVienModel model = null;
		
		try {
			String sql = "SELECT * FROM THANHVIEN WHERE TENDN=? AND MATKHAU=?";
			PreparedStatement ps = DataUtil.getConnection().prepareStatement(sql);
			ps.setString(1, tenDangNhap);
			ps.setString(2, matKhau);
			ResultSet rs = ps.executeQuery();
			
			while(rs.next()) {
				model = new ThanhVienModel();
				
				model.setId(rs.getString("id"));
				model.setTenDangNhap(rs.getString("TenDN"));
				model.setMatKhau(rs.getString("MatKhau"));
				model.setVaiTro(rs.getString("VaiTro"));
				model.setGhiChu(rs.getString("GhiChu"));
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return model;
	}

	public static ArrayList<ThanhVienModel> getAllThanhVien() {
		ArrayList<ThanhVienModel> listThanhVien = new ArrayList<ThanhVienModel>();
		
		try {
			String sql  = "select * from thanhvien";
			PreparedStatement ps = DataUtil.getConnection().prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				ThanhVienModel model = new ThanhVienModel();
				
				model.setId(rs.getString("id"));
				model.setTenDangNhap(rs.getString("TenDN"));
				model.setMatKhau(rs.getString("MatKhau"));
				model.setVaiTro(rs.getString("VaiTro"));
				model.setGhiChu(rs.getString("GhiChu"));
				
				listThanhVien.add(model);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		return listThanhVien;
	}
}

